package oj_leetcode;

import java.util.LinkedList;

public class ReverseNodesInKGroup {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode begin = dummy;
        LinkedList<ListNode> curr = new LinkedList();
        int count = 1;
        while(head != null && count <= k){
            curr.add(head);
            head = head.next;
            count++;
        }
        if(count > k){
            while(curr.isEmpty()){
                ListNode node = curr.pollLast();
                begin.next = node;
                begin = node;
            }
            begin.next = reverseKGroup(head, k);
            return dummy.next;
        }else{
            return dummy.next;
        }
    }

    public static void main(String[] args) {

    }
}
